package cn.cmkj.auction.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.ex.DbException;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import cn.cmkj.auction.R;
import cn.cmkj.auction.app.BaseApplication;
import cn.cmkj.auction.event.LoginEvent;
import cn.cmkj.auction.ui.AboutUs;
import cn.cmkj.auction.ui.AboutUs;
import cn.cmkj.auction.ui.LoginActivity;
import cn.cmkj.auction.ui.MyCouponListActivity;
import cn.cmkj.auction.ui.MyFavListActivity;
import cn.cmkj.auction.ui.MyOrderListActivity;
import cn.cmkj.auction.ui.MyPaimaiListActivity;
import cn.cmkj.auction.ui.UserAddressActivity;
import cn.cmkj.auction.ui.UserInfoActivity;
import cn.cmkj.auction.widget.AlertDialog;

/**
 * Created by cunguoyao on 2017/8/13.
 */
@ContentView(R.layout.fragment_main_mine)
public class MainMineFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, View.OnClickListener {

    @ViewInject(R.id.swipeRefreshLayout)
    private SwipeRefreshLayout swipeRefreshLayout;
    @ViewInject(R.id.scrollView)
    private ScrollView scrollView;
    @ViewInject(R.id.account_image_frame)
    private RelativeLayout accountImageFrame;
    @ViewInject(R.id.account_image)
    private ImageView accountAvatarImage;
    @ViewInject(R.id.account_name)
    private TextView accountNameText;

    @ViewInject(R.id.mine_text_coupon)
    private TextView textViewCoupon;
    @ViewInject(R.id.mine_text_fav)
    private TextView textViewFav;

    @ViewInject(R.id.mine_my_order)
    private RelativeLayout myOrderLayout;
    @ViewInject(R.id.mine_wait_pay)
    private LinearLayout myOrderWaitPay;
    @ViewInject(R.id.mine_wait_deliver)
    private LinearLayout myOrderWaitDeliver;
    @ViewInject(R.id.mine_wait_receive)
    private LinearLayout myOrderWaitReceive;
    @ViewInject(R.id.mine_paimai)
    private LinearLayout myOrderPaimai;

    @ViewInject(R.id.mine_address)
    private RelativeLayout mineAddress;
    @ViewInject(R.id.mine_about_us)
    private RelativeLayout mineAboutus;
    @ViewInject(R.id.btn_logout)
    private Button logoutBtn;
    @ViewInject(R.id.layout_logout)
    private RelativeLayout logoutLayout;

    public static MainMineFragment create() {
        MainMineFragment f = new MainMineFragment();
        Bundle args = new Bundle();
        //args.putInt("type", type);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = x.view().inject(this, inflater, container);
        scrollView.getViewTreeObserver().addOnScrollChangedListener(new  ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                swipeRefreshLayout.setEnabled(scrollView.getScrollY()==0);
            }
        });
        account = getAccount();
        if(account != null) {
            accountNameText.setText(account.getUsername());
            logoutLayout.setVisibility(View.VISIBLE);
        }else {
            logoutLayout.setVisibility(View.GONE);
        }
        //为SwipeRefreshLayout设置监听事件
        swipeRefreshLayout.setOnRefreshListener(this);
        accountImageFrame.setOnClickListener(this);
        accountNameText.setOnClickListener(this);
        textViewCoupon.setOnClickListener(this);
        textViewFav.setOnClickListener(this);
        myOrderLayout.setOnClickListener(this);
        myOrderWaitPay.setOnClickListener(this);
        myOrderWaitDeliver.setOnClickListener(this);
        myOrderWaitReceive.setOnClickListener(this);
        myOrderPaimai.setOnClickListener(this);
        mineAddress.setOnClickListener(this);
        mineAboutus.setOnClickListener(this);
        logoutBtn.setOnClickListener(this);
        return view;
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //结束后停止刷新
                swipeRefreshLayout.setRefreshing(false);
            }
        }, 2000);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.account_image_frame:
                jump(UserInfoActivity.class);
                break;
            case R.id.mine_text_coupon:
                Intent iMineIntent1 = new Intent(getActivity(), MyCouponListActivity.class);
                startActivity(iMineIntent1);
                break;
            case R.id.mine_text_fav:
                Intent iMineIntent2 = new Intent(getActivity(), MyFavListActivity.class);
                startActivity(iMineIntent2);
                break;
            case R.id.mine_my_order:
                Intent i1 = new Intent(getActivity(), MyOrderListActivity.class);
                i1.putExtra("index", 0);
                startActivity(i1);
                break;
            case R.id.mine_wait_pay:
                Intent i2 = new Intent(getActivity(), MyOrderListActivity.class);
                i2.putExtra("index", 1);
                startActivity(i2);
                break;
            case R.id.mine_wait_deliver:
                Intent i3 = new Intent(getActivity(), MyOrderListActivity.class);
                i3.putExtra("index", 2);
                startActivity(i3);
                break;
            case R.id.mine_wait_receive:
                Intent i4 = new Intent(getActivity(), MyOrderListActivity.class);
                i4.putExtra("index", 3);
                startActivity(i4);
                break;
            case R.id.mine_paimai:
                Intent iMineIntent0 = new Intent(getActivity(), MyPaimaiListActivity.class);
                startActivity(iMineIntent0);
                break;
            case R.id.mine_address:
                startActivity(new Intent(getActivity(), UserAddressActivity.class));
                break;
            case R.id.mine_about_us:
                startActivity(new Intent(getActivity(), AboutUs.class));
                break;
            case R.id.btn_logout:
                AlertDialog loginDialog = new AlertDialog(getActivity());
                loginDialog.builder().setTitle("提示").setMsg("确认要退出当前账号吗？")
                        .setCancelable(true).setNegativeButton("取消", null)
                        .setPositiveButton("确认", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                account.setDefaultAccount(0);
                                try {
                                    BaseApplication.getInstance().getDbManager().saveOrUpdate(account);
                                } catch (DbException e) {
                                    e.printStackTrace();
                                }
                                EventBus.getDefault().post(new LoginEvent(LoginEvent.LOGOUT_SUCCESS));
                            }
                        }).show();
                break;
        }
    }

    private void jump(Class c) {
        if(getAccount() == null) {
            AlertDialog loginDialog = new AlertDialog(getActivity());
            loginDialog.builder().setTitle("提示").setMsg("需要登录才能使用此功能")
                    .setCancelable(true).setNegativeButton("取消", null)
                    .setPositiveButton("立即登录", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(getActivity(), LoginActivity.class);
                            startActivity(intent);
                        }
                    }).show();
        }else {
            Intent intent = new Intent(getActivity(), c);
            startActivity(intent);
        }
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onEventBus(LoginEvent event){
        Toast.makeText(getActivity(), "event.getType()=" + event.getType(), Toast.LENGTH_SHORT).show();
        if (event.getType() == LoginEvent.LOGIN_SUCCESS) {
            account = getAccount();
            if(account != null) {
                accountNameText.setText(account.getUsername());
                logoutLayout.setVisibility(View.VISIBLE);
            }
        }else if (event.getType() == LoginEvent.LOGOUT_SUCCESS) {
            swipeRefreshLayout.scrollTo(0,0);
            accountNameText.setText("登录/注册");
            logoutLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
